package pers.xhh.shopapi.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import pers.xhh.shopapi.annotation.UserLoginToken;
import pers.xhh.shopapi.common.Mi;
import pers.xhh.shopapi.entity.to.goods.MiGoodsComTo;
import pers.xhh.shopapi.entity.to.goods.MiGoodsTo;
import pers.xhh.shopapi.service.MiGoodsService;

import javax.validation.Valid;

/**
 * @author wei-xhh
 * @date 2020-11-26
 */
@CrossOrigin
@RestController
@RequestMapping("/api/v1")
public class MiGoodsController {

    @Autowired
    MiGoodsService miGoodsService;

    @UserLoginToken
    @GetMapping("/goods")
    public Mi listGoods(@RequestParam(required = false) String query,
                        @RequestParam Integer pagenum,
                        @RequestParam Integer pagesize) {
        return miGoodsService.listGoods(query, pagenum, pagesize);
    }

    @UserLoginToken
    @PostMapping("/goods")
    public Mi saveGoods(@Valid @RequestBody MiGoodsTo miGoodsTo) {
        return miGoodsService.saveGoods(miGoodsTo);
    }

    @UserLoginToken
    @GetMapping("/goods/{id}")
    public Mi getGoodsById(@PathVariable("id") Integer id) {
        return miGoodsService.getGoodsById(id);
    }

    @UserLoginToken
    @PutMapping("/goods/{id}")
    public Mi updateGoodsById(@PathVariable("id") Integer id,
                              @Valid @RequestBody MiGoodsTo miGoodsTo) {
        return miGoodsService.updateGoodsById(id, miGoodsTo);
    }

    @UserLoginToken
    @PutMapping("/goods/common/{id}")
    public Mi commonUpdate(@PathVariable("id") Integer id,
                              @Valid @RequestBody MiGoodsComTo miGoodsTo) {
        return miGoodsService.commonUpdate(id, miGoodsTo);
    }

    @UserLoginToken
    @DeleteMapping("/goods/{id}")
    public Mi deleteById(@PathVariable("id") Integer id) {
        return miGoodsService.deleteById(id);
    }

    @UserLoginToken
    @PostMapping("/upload")
    public Mi uploadFilePic(@RequestParam("file") MultipartFile file) {
        return miGoodsService.uploadFilePic(file);
    }
}
